<?php

namespace Home\Controller;
use Common\Controller\FirstendController;

class InvalController extends FirstendController {
 
    public function index() {
		$id = I('id','', 'intval');
        !$id && $this->_404();

		$union_mod = D('Union');
		$user_mod = D('User');
		$user = $user_mod->where(array('id' => $id))->find();
		if(!$user) redirect(C('itk_site_url'));	// 如果用户不存在则无积分。
		$date['uid']		= $user['id'];
		$date['username']	= $user['username'];
		$date['score']		= C('itk_score_rule.union_visit');

		if (false === $data = $union_mod->create($date)) {
            $this->error($union_mod->getError());
        }
        if (!$union_mod->where(array('ip'=>get_client_ip()))->count()) {
			$score_data = array('score'=>array('exp','score+'.C('itk_score_rule.union_visit')), 'sign_time'=>strtotime(date('Ymd')));
			D('User')->where(array('id'=>$user['id']))->setField($score_data); 
		
			$ScoreLog_data['uid']		=	$user['id'];
			$ScoreLog_data['uname']	=	$user['username'];
			$ScoreLog_data['action']	=	'union_visit';
			$ScoreLog_data['score']	=	C('itk_score_rule.union_visit');
			D('ScoreLog')->create($ScoreLog_data);
			D('ScoreLog')->add();
			$union_mod->add();
        } 
		redirect(C('itk_site_url'));
    }
}